# 导包 (todo 直接报错,因为这是老版本的)
import pandas as pd
import numpy as np
# todo 1.获取数据
# 注意: 导包(todo
data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]
print(data.shape)
print(target.shape)


from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test = train_test_split(data, target, test_size=0.2, random_state=0)

# todo 3.数据标准化处理
from sklearn.preprocessing import StandardScaler

ss = StandardScaler()

new_x_train = ss.fit_transform(x_train)
new_x_test = ss.transform(x_test)
# todo 4.创建模型(正规方程法)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
# todo 5. 模型训练
model.fit(new_x_train,y_train)
print(f"训练后参数:{model.coef_}")
print(f"训练后intercept_参数:{model.intercept_}")
# todo 6.模型预测
y_pred = model.predict(new_x_test)
# todo 7. 模型评估
from sklearn.metrics import mean_squared_error,mean_absolute_error
print(f"均方误差:{mean_squared_error(y_test,y_pred)}")
print(f"均方根误差:{mean_squared_error(y_test,y_pred,squared = False)}")
print(f"平均绝对误差:{mean_absolute_error(y_test,y_pred,squared = False)}")
